<?php
// +-------------------------------------------------------------------
// |
// +-------------------------------------------------------------------
// | Copyright (c) 2009-2016 All rights reserved.
// +-------------------------------------------------------------------
namespace Service\User;

class EyesMoneyLog extends \Service\Common\BaseModel
{
    protected $name = 'eyes_money_log';

    const MONEY_LOG_TYPE_ADD_COUPON = 0;//获得券
    const MONEY_LOG_TYPE_USE_COUPON   = 1;//消耗券
    const MONEY_LOG_TYPE_ADD_MONEY   = 2;//获得币
    const MONEY_LOG_TYPE_USE_MONEY   = 3;//消耗币
    const MONEY_LOG_TYPE_ADMIN_ADD_MONEY   = 4;//充币
    const MONEY_LOG_TYPE_ADMIN_DOCK_MONEY   = 5;//后台扣币
    const MONEY_LOG_TYPE_ADMIN_ADD_COUPON   = 6;//后台充券
    const MONEY_LOG_TYPE_COUPON   = 0;//券
    const MONEY_LOG_TYPE_MONEY   = 1;//币

    const TYPE_LABEL = [
        self::MONEY_LOG_TYPE_ADD_COUPON => '分享收益',
        self::MONEY_LOG_TYPE_USE_COUPON => '测试消耗',
        self::MONEY_LOG_TYPE_ADD_MONEY => '任务收益',
        self::MONEY_LOG_TYPE_USE_MONEY => '兑换礼品扣除',
        self::MONEY_LOG_TYPE_ADMIN_ADD_MONEY => '后台充币',
        self::MONEY_LOG_TYPE_ADMIN_DOCK_MONEY => '后台扣币',
        self::MONEY_LOG_TYPE_ADMIN_ADD_COUPON => '后台充券',
        self::MONEY_LOG_TYPE_COUPON => '券',
        self::MONEY_LOG_TYPE_MONEY => '币',
    ];


    //资金明细
    public function moneyList($uid, $page_num = 1, $page_size = 5)
    {
        $where = [
            'uid' => $uid,
        ];
        $list = (array)$this->where($where)->order('id desc')->page($page_num, $page_size)->select();
        foreach ($list as &$row) {
            $row['type_label'] = self::TYPE_LABEL[$row['typeid']];
        }
        $count = $this->where($where)->count();
        return [
            $list,
            $page_count = $count,
            $page_total = ceil($count / $page_size),
        ];
    }

    //后台充币
    public function recharge($uid, $money, $remarks)
    {
        return $this->changeEyesMoney($uid, $money, $remarks,self::MONEY_LOG_TYPE_MONEY,self::MONEY_LOG_TYPE_ADMIN_ADD_MONEY,0);
    }

    //后台充券
    public function addcoupon($uid, $money, $remarks)
    {
        return $this->changeEyesMoney($uid, $money, $remarks,self::MONEY_LOG_TYPE_COUPON,self::MONEY_LOG_TYPE_ADMIN_ADD_COUPON,0);
    }

    //后台扣币
    public function dock($uid, $money, $remarks)
    {
        return $this->changeEyesMoney($uid, -$money,$remarks, self::MONEY_LOG_TYPE_MONEY,self::MONEY_LOG_TYPE_ADMIN_DOCK_MONEY,0);
    }

    //更新额度
    protected function changeEyesMoney($uid, $change_money,$remarks, $type, $typeid ,$taskid = 0)
    {
        $user = OE('user')->getUserInfo($uid);

        if($type == self::MONEY_LOG_TYPE_MONEY){
            $field = 'eyes_money';
        }else{
            $field = 'eyes_coupon';
        }
        if ($change_money < 0) {
            if (abs($change_money) > $user['eyes_money']) {
                throw new \Exception("余额不足");
            }
            //-余额
            $new_balance = $user[$field] - abs($change_money);

        } else {
            $new_balance = $user[$field] + $change_money;

        }

        if ($change_money != 0) {
            $update = M('user')->where(['uid' => $uid])->save([
                $field => $new_balance,
                'update_time' => date('Y-m-d H:i:s')
            ]);
            if ($update === false) {
                throw new \Exception("额度更新失败");
            }
        }
        //额度变更日志
        $insert_id = $this->add([
            'userid' => $uid,
            'type' => $type,
            'typeid' => $typeid,
            'taskid' => $taskid,
            'change_money' => $change_money,
            'old_balance' => $user[$field],
            'new_balance' => $new_balance,
            'remarks' => $remarks,
            'create_time' => date('Y-m-d H:i:s')
        ]);
        if (!$insert_id) {
            throw new \Exception("资金日志记录失败");
        }

        return true;
    }
}